$(function() {

	//点击推荐商品，进入商品详情
	$("#recommend_items").on("click", ".go_item", function() {
		var goodsid = $(this).attr("data-id");
		if (goodsid == '') {
			layer.msg("数据异常");
		} else {
			var target = encodeURI("./xiangqing.html?id=" + goodsid);
			location.href = target;
		}
	})

	//推荐列表
	$.ajax({
		type: "POST",
		url: "http://127.0.0.1:8080/goods/findRecommendGoodsList",
		dataType: "json",
		xhrFields: {
			withCredentials: true
		},
		success: function(data) {
			console.log(data)
			var code = data.code;
			var node = $("#recommend_items");
			if (code == 'success') {
				var goodsList = data.data;
				var html = "";
				goodsList.forEach(function(obj) {
					var goods = obj.goods;
					html += '<div class="remen fl">' +
						'<div class="tu"><a href="javascript:void(0)" ' +
						'class="go_item" data-id="' + goods.id + '">' +
						'<img src="' + goods.imgUrl +
						'" width="150" height="150"></div>' +
						'<div class="miaoshu">' + goods.goodsName +
						'</div><div class="jiage">' + goods.price + '元</div>' +
						'</a><div class="piao"><span>推荐指数：' + obj.grade +
						'</span></div></div>';
				})
				node.html(html);
			} else {
				node.html("<h4>暂无推荐商品</h4>");
			}
		}
	});

	//点击精品，转详情
	$("#boutiqueGoods").on("click", ".gotoXq", function() {
		var id = $(this).attr("data-id");
		if (id == '') {
			layer.msg("数据异常");
		} else {
			var target = encodeURI("./xiangqing.html?id=" + id);
			location.href = target;
		}
	})

	//精品列表
	$.ajax({
		type: "POST",
		url: "http://127.0.0.1:8080/goods/findBoutiqueGoods",
		dataType: "json",
		success: function(data) {
			var code = data.code;
			if (code == 'success') {
				var obj = data.data;
				var html = "";
				obj.forEach(function(goods) {
					html += '<div class="mingxing fl"><div class="sub_mingxing">' +
						'<a href="javascript:void(0)" data-id="' + goods.id + '" class="gotoXq">' +
						'<img src="' + goods.imgUrl + '" alt=""></div><div class="pinpai">' +
						goods.goodsName + '</a></div><div class="youhui">' +
						goods.description + '</div><div class="jiage">' +
						goods.price + '元</div></div>';
				});
				$("#boutiqueGoods").html(html);
			}
		}
	});

	//获取登陆用户session
	$.ajax({
		type: "POST",
		url: "http://127.0.0.1:8080/user/getSession",
		dataType: "json",
		xhrFields: {
			withCredentials: true
		},
		success: function(data) {
			var code = data.code;
			if (code == 'success') {
				var node = $("#login_but");
				node.empty();
				var html = '<li style="color:#ffffff">' + data.data.userName + '，欢迎回来</li>';
				html += '<li>|</li><li><a href="javascript:void(0)" id="goOrderList">我的订单</a></li>';
				html += '<li>|</li><li><a href="javascript:void(0)" id="logout">退出</a></li>';
				node.html(html);
			}
		}
	});

	$("#login_but").on("click", "#goOrderList", function() {
		window.location.href = './dingdanzhongxin.html';
	})

	//退出登录
	$("#login_but").on("click", "#logout", function() {
		$.ajax({
			type: "GET",
			url: "http://127.0.0.1:8080/user/logout",
			dataType: "json",
			xhrFields: {
				withCredentials: true
			},
			success: function(data) {
				var code = data.code;
				if (code == 'success') {
					window.location.href = './index.html';
				}
			}
		});
	})

});
